package com.hry.store.cms.controller;

import com.github.pagehelper.PageInfo;
import com.hry.store.cms.common.Const;
import com.hry.store.cms.common.ServerResponse;
import com.hry.store.cms.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 带有分页查询所有会员信息
     * @param modelMap
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("user/member")
    public String userMember(ModelMap modelMap, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        PageInfo userList = memberService.getAll(pageNum, pageSize);
        modelMap.put("userList",userList);
        return "member";
    }

    /**
     * 修改用户信息
     */
    @RequestMapping("user/editUserInfo")
    @ResponseBody
    public ServerResponse editUserInfo(@RequestParam("userName") String userName, @RequestParam("userEmail") String userEmail,
                                       @RequestParam("userPhone") String userPhone, @RequestParam("userSign") String userSign,
                                       @RequestParam("favorite") String favorite, @RequestParam("editUserStatus") Integer editUserStatus) {

        int resultCount = memberService.updateUserInfo(userName, userEmail, userPhone, userSign, favorite, editUserStatus);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("修改成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "修改失败");
    }

}
